<template>
  <div>
    <RHTreeTable
      :RHTable="table"
      :RHTree="tree"
      :nodeClickCallBack="nodeClickCallBack"
      ref="RH_tree_table"
    ></RHTreeTable>
    <moduleForm :RHDialog="dialog" ref="r_h_dialog"></moduleForm>
  </div>
</template>
<script>
import RHTreeTable from "@/common/TreeTable/index.vue";
import moduleForm from "@/views/module/form/form.vue";
import _ from "lodash";
import * as service from "@/api/module";

export default {
  name: "module_index",
  data: function() {
    return {
      table: {
        url: this.$store.state.url.user + "api/users/renHaiModule/getList",
        header: {
          show: true,
          btn: [{ value: "新增", click: this.show, param: { moduleId: "" } }]
        },
        params: { moduleId: "" },
        columns: [
          { fieId: "moduleId", title: "ID" },
          { fieId: "moduleName", title: "模块名称" },
          { fieId: "moduleUrl", title: "地址" },
          { fieId: "createTime", title: "创建时间" },
          { fieId: "updateTime", title: "修改时间", hide: true },
          {
            title: "test",
            hide: true,
            formatter: function(row) {
              return row.modulePid.moduleId;
            }
          }
        ],
        columnsBtn: [
          { value: "修改", click: this.updatePre, size: "mini" },
          { value: "删除", click: this.delete, size: "mini" }
        ],
        // 分页
        paging: true
      },
      tree: {
        key: "moduleId",
        url:
          this.$store.state.url.user +
          "api/users/renHaiModule/getListTree.action",
        title: "模块树",
        defaultProps: {
          children: "children",
          label: "moduleName"
        },
        titleClick: this.titleClick
      },
      dialog: {
        title: "添加模块",
        dialogVisible: false,
        btn: [
          {
            name: "取消",
            click: this.cancel
          },
          {
            name: "确定",
            type: "primary",
            click: this.edit
          }
        ]
      }
    };
  },
  methods: {
    //编辑 修改添加
    edit(param) {
      const that = this;
      service.saveUpdate(param.data).then(function(data) {
        if (data && data.state === 1) {
          that.$util.message(data);
        }
        //刷新表格
        that.$refs.RH_tree_table.tableRefresh({
          moduleId: param.data.modulePid
        });
        //刷新树
        that.$refs.RH_tree_table.treeRefresh(
          {
            moduleId: param.data.modulePid
          },
          true
        );
        //关闭弹窗
        that.$refs.r_h_dialog.close();
      });
    },
    //关闭弹出层
    cancel() {
      const that = this;
      that.$refs.r_h_dialog.close();
    },
    //打开弹出层
    show(param) {
      this.$refs.r_h_dialog.getInfo(param.moduleId);
      this.$refs.r_h_dialog.show();
    },
    //修改表单
    updatePre(param) {
      //打开弹出层
      this.show({ moduleId: param.moduleId });
    },
    // 删除
    delete(param) {
      const that = this;
      that.$util.messageBox({ msg: "确定删除？" }, function() {
        service.del({ moduleId: param.moduleId }).then(function(data) {
          if (data && data.state === 1) {
            that.$util.message(data);
          }
          //属性表格
          that.$refs.RH_tree_table.tableRefresh({
            moduleId: param.modulePid
          });
          //刷新树
          that.$refs.RH_tree_table.treeRefresh(
            {
              moduleId: param.modulePid
            },
            true
          );
        });
      });
    },
    nodeClickCallBack(data, param) {
      let moduleId = "";
      let moduleName = "无";
      if (data && data.moduleId && _.toInteger(data.moduleGrade) !== 2) {
        moduleId = data.moduleId;
        moduleName = data.moduleName;
        if (moduleName === "") {
          moduleName = "无";
        }
      }
      if (_.toInteger(data.moduleGrade) === 1) {
        this.$refs.r_h_dialog.formInitialize({
          modulePid: moduleId,
          modulePName: moduleName
        });
        return true;
      }
      return false;
    },
    titleClick() {
      this.$refs.r_h_dialog.formInitialize({
        modulePid: "",
        modulePName: "无"
      });
    }
  },
  components: {
    moduleForm,
    RHTreeTable
  }
};
</script>

<style scoped></style>
